package com.baizhi.controller;

import com.baizhi.entity.Patient;
import com.baizhi.service.PatientService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("patient")
public class PatientController {
    @Autowired
    private PatientService patientService;

    @RequestMapping("show")
    public Map show(String name,Integer page,Integer limit){
        Map map = new HashMap();
        PageInfo<Patient> pageInfo = patientService.selectByPage(name, page, limit);
        map.put("code",0);
        map.put("msg","");
        map.put("count",pageInfo.getTotal());
        map.put("data",pageInfo.getList());
        return map;
    }
    @RequestMapping("add")
    public Map addPatient(Patient patient){
        Map map = new HashMap();
        try {
            patientService.insertPatient(patient);
            map.put("flag",true);
        } catch (Exception e) {
            map.put("flag",false);
        }
        return map;
    }
    @RequestMapping("remove")
    public Map removePatient(Integer pid){
        Map map = new HashMap();
        try {
            patientService.deletePatient(pid);
            map.put("flag",true);
        } catch (Exception e) {
            map.put("flag",false);
        }
        return map;
    }
    @RequestMapping("update")
    public Map updatePatient(Patient patient){
        System.out.println(patient);
        Map map = new HashMap();
        try {
            patientService.updatePatient(patient);
            map.put("flag",true);
        } catch (Exception e) {
            map.put("flag",false);
        }
        return map;
    }
    @RequestMapping("getOne")
    public Patient getOnePatient(Integer pid){
        Patient patient = patientService.selectById(pid);
        System.out.println(patient);
        return  patient;
    }
}
